/* Arduino touch library v0.2
 * Copyright (C) 2011 Manfred Fuchs
 * www.mafu-foto.de
 * manfred@fuchsrudel.de
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef TouchLib_h
#define TouchLib_h

#include <MI0283QT2/MI0283QT2.h>
/**
 * max length of button caption
 */
#define TOUCH_MAX_BUTTON_CAPTION_LEN  32

/**
 * size of button shadow
 */
#define BUTTON_SHADOW  2

/**
 * color macros
 */
#define RGB_R(rgb)   ((rgb>>8)&0x00F8)
#define RGB_G(rgb)   ((rgb>>3)&0x00FC)
#define RGB_B(rgb)   ((rgb<<3)&0x00F8)

/**
 * define pointer type for LCD and panel
 */
typedef MI0283QT2 *lcdPtr;
typedef ADS7846   *tpPtr;

/**
 * button format struct
 */
typedef struct
{
  void      *next;
  uint16_t  left;
  uint16_t  top;
  uint16_t  width;
  uint16_t  height;
  uint16_t  col_normal;
  uint16_t  col_down;
  uint16_t  col_text;
  uint16_t  tag;
  uint8_t   size;
  uint8_t   group;
  bool      round;
  bool      toggle;
  bool      down;
  char      caption[TOUCH_MAX_BUTTON_CAPTION_LEN+1];
} TouchButton;
typedef TouchButton *TouchButtonPtr;

class Touch
{
  private:
    lcdPtr         lcd;
    tpPtr          tp;
    uint8_t        pressure;
    TouchButtonPtr buttons;
    TouchButtonPtr startBtn;
    TouchButtonPtr lastBtn;
    bool           pressed;
    uint16_t       startX;
    uint16_t       startY;
    uint16_t       posX;
    uint16_t       posY;
    void           buttonDraw(TouchButtonPtr _button, bool _down);
    void           buttonDrawRect(uint16_t _l, uint16_t _t, uint16_t _w, uint16_t _h, uint16_t _c);
    void           buttonDrawRound(uint16_t _l, uint16_t _t, uint16_t _w, uint16_t _h, uint16_t _c);
    TouchButtonPtr buttonFromPos(uint16_t _x, uint16_t _y);
    void           buttonState(TouchButtonPtr _current);
    bool           checkPressure(bool _pressed);
  public:
    Touch(lcdPtr _lcd, tpPtr _tp, uint8_t _pressure);
    TouchButtonPtr buttonAdd(uint16_t _left, uint16_t _top, uint16_t _width, uint16_t _height, uint16_t _col_normal, uint16_t _col_down, uint16_t _col_text, bool _round, bool _toggle, uint8_t _group, uint8_t _size, char *_caption, uint16_t _tag);
    bool           buttonDown(TouchButtonPtr _button);
    TouchButtonPtr buttonGetFromTag(uint16_t _tag);
    void           buttonSetDown(TouchButtonPtr _button, bool _down);
    void           buttonsClear();
    void           buttonsDraw();
    TouchButtonPtr check();
};

#endif
